package conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class conexion {
    //static Connection cnn = null;
    Connection cnn = null;
    Statement inst = null;
    static String strcon = "jdbc:mysql://localhost:3306/";
    static String DB = "analisis2";
    static String us = "root";
    static String pass = "";

    public conexion(){
        strcon = "jdbc:mysql://localhost:3306/"+DB;
    }

    public Boolean Conectar() throws ClassNotFoundException, SQLException{
        boolean b = false;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            cnn = DriverManager.getConnection(strcon,us, pass);
            if (cnn!=null){
                cnn.close();
                b=true;
            }
        }catch (SQLException ex){
            b = false;
            String error = ex.getMessage();
            error = ex.toString();
            error = ex.getSQLState();
            error = ex.getLocalizedMessage();
            String error2 = error;
        }
        return b;
    }

    /**
     * Cierra la Sesión
     */
    public void CerrarSesion(){
        try{
            if (cnn!=null){
                cnn.close();
            }
        }catch(Exception exx){
        }
    }

    public Boolean ModificarTabla(String cond) throws ClassNotFoundException{
        boolean ret = false;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch (InstantiationException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            cnn = DriverManager.getConnection(strcon, us, pass);
            if (cnn!=null){
                try {
                    inst = (Statement) cnn.createStatement();
                    inst.executeUpdate(cond);
                }
                catch (SQLException ex) {
                    Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        catch (SQLException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }

    public ResultSet ConsultarTabla(String cond) throws ClassNotFoundException{
        ResultSet consulta = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } 
        catch (InstantiationException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            cnn = DriverManager.getConnection(strcon, us, pass);
            if (cnn!=null){
                try {
                    inst = (Statement) cnn.createStatement();
                    consulta = inst.executeQuery(cond);
                } 
                catch (SQLException ex) {
                    Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        catch (SQLException ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return consulta;
    }    
}
